package hydrap2p.upload;

import hydrap2p.ServiceLocation;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class UploadProtocolThrift implements UploadProtocolIface {

	private UploadProtocol.Client connection;
	private ServiceLocation location;
	
	public UploadProtocolThrift(ServiceLocation location) throws TException
	{
		if (location==null)
			throw new TException("Invalid service location");
		this.location=location;

		TTransport transport = new TSocket(location.address,location.port);
		transport.open();
		connection=new UploadProtocol.Client(new TBinaryProtocol(transport));
		
		if (!connection.supportedInterfaces().contains("UploadProtocol"))
			throw new TException("Module does not implement UploadProtocol");
	}
	
	@Override
	public void cancelUpload(UploadQueued upload) throws Exception {
		connection.cancelUpload(upload);

	}

	@Override
	public void upload(UploadQueued upload) throws Exception {
		connection.upload(upload);
	}

	public ServiceLocation getLocation()
	{
		return location;
	}
}
